package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_member_entrepreneur;
import cn.com.api.entity.ApiT_member_wemedia;
import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_circle;
import cn.com.api.entity.ApiU_favorite;
import cn.com.api.entity.ApiU_follow;
import cn.com.api.entity.U_article;
import cn.com.api.service.ApiMemberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiT_member_wemediaService;
import cn.com.api.service.ApiU_circleService;
import cn.com.api.service.ApiU_favoriteService;
import cn.com.api.service.ApiU_followService;
import cn.com.api.service.CheckService;
import cn.com.api.service.U_articleService;
import cn.com.util.*;

/*
 * 关注api控制器
 */
@Controller
@RequestMapping(Utils.SYS_API + "follow/")
public class ApiU_FollowController {

	@Autowired
	private ApiU_followService apiU_followService;
	@Autowired
	private U_articleService u_articleService;
	@Autowired
	private CheckService checkService;
	@Autowired
	private ApiU_circleService apiU_circleService;
	@Autowired
	private ApiMemberService apiMemberService;
	@Autowired
	private ApiT_member_entrepreneurService apiT_member_entrepreneurService;
	@Autowired
	private ApiT_member_wemediaService apiT_member_wemediaService;

	private static Logger logger = Logger.getLogger(ApiU_orgController.class);

	/**
	 * 关注和取消关注文章
	 * 
	 * @param conn_id
	 *            关注的id 关注文章时，传发文章人的ID(creater_id)
	 * @param status
	 *            关注还是取消关注（1-关注；0-取消关注）
	 * @param type
	 *            关注和取消关注的类型(0个人 1企业主 2小圈 3自媒体(文章) 4新发现类别 5新发现信息)
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 */
	@RequestMapping(value = "addOrDelFollow")
	public void addOrDelFollow(HttpServletRequest request, HttpServletResponse response, String conn_id, String status,
			String user_id, String type, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 删除
			Map param = new HashMap();
			param.put("conn_id", conn_id);
			param.put("type", type);
			param.put("user_id", user_id);
			apiU_followService.deleteU_followByMap(param);
			// 添加收藏
			if ("1".equals(status)) {
				ApiU_follow apiU_follow = new ApiU_follow();
				apiU_follow.setConn_id(Integer.valueOf(conn_id));
				apiU_follow.setType(Integer.valueOf(type));
				apiU_follow.setUser_id(Integer.valueOf(user_id));
				apiU_follow.setAdd_time(Util.CurrentTime());
				apiU_followService.insertU_follow(apiU_follow);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作完成");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 关注列表--获取关注
	 * 
	 * @param type
	 *            (0个人 1企业主 2小圈 3自媒体(文章) 4新发现类别 5新发现信息)
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param currentPage
	 *            页数
	 * @param showCount
	 *            每页显示数
	 * @return
	 */
	@RequestMapping(value = "MyFollowListByType")
	public void MyFollowListByType(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String type, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> params = new HashMap<String, String>();
			params.put("u_id", user_id);
			params.put("token", token);
			params.put("type", type);

			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(params, map)) {
				params.clear();
				switch (type) {
				case "0":// 个人
					List<ApiTmember> member_list = this.apiMemberService.findFollowMemberList(user_id, 0, page);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, member_list);
					map.put(Utils.Page, page);
					break;
				case "1":// 企业主
					List<ApiT_member_entrepreneur> entre_list = this.apiT_member_entrepreneurService
							.findEntreListByFollowType(user_id, 1, page);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, entre_list);
					map.put(Utils.Page, page);
					break;
				case "2": // 小圈
					List<ApiU_circle> circle_list = this.apiU_circleService.findFollowCircleList(user_id, 2, page);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, circle_list);
					map.put(Utils.Page, page);
					break;
				case "3": // 自媒体
					List<ApiT_member_wemedia> wemedia_list = this.apiT_member_wemediaService
							.findFollowWemediaList(user_id, 3, page);
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作完成");
					map.put(Utils.DATAS, wemedia_list);
					map.put(Utils.Page, page);
					break;
				default:
					map.put(Utils.RESULT, "0003");
					map.put(Utils.MESSAGE, "分类不存在！");
					break;
				}
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
}
